using System;
using Newtonsoft.Json;

namespace SlickGridNet.Serialization
{
	/// <summary>
	/// Converts IExplicitObject to JSON (used in Newtonsoft.Json).
	/// <para>Serialize target object as value of <see cref="IExplicitObject.ExplicitName"/> if defined, 
	/// otherwise <see cref="Type.FullName"/> of object will be used.</para>
	/// </summary>
	public class ExplicitObjectConverter : JsonConverter
	{
		public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
		{
			var explicitObject = value as IExplicitObject;
			if (explicitObject == null)
				throw new ArgumentException("ExplicitObjectConverter can convert value only of IExplicitObject type.");
			var explicitName = explicitObject.ExplicitName;
			if (explicitName == null)
			{
				var type = value.GetType();
				explicitName = type.FullName;
			}
			writer.WriteRawValue(explicitName);
		}

		public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
		{
			throw new NotImplementedException();
		}

		public override bool CanConvert(Type objectType)
		{
			throw new NotImplementedException();
		}

		public override bool CanRead
		{
			get { return false; }
		}
	}
}